// 生成成本单
<template>
  <view class="page">
    <mesSearch
      @search="search"
      :placeholder="placeholder"
      v-model="keywords"
    ></mesSearch>
    <mes-collapse :form="collapseForm"></mes-collapse>
    <mes-tip v-if="noData"></mes-tip>
  </view>
</template>

<script>
import mesCollapse from '../../../components/mes/mes-collapse.vue'
export default {
  components: { mesCollapse },
  data() {
    return {
      noData: false,
      placeholder: '单据编号',
      keywords: '',
      page: 1,
      hasMore: true,
      collapseForm: {
        // 自定义组件类名（可不传）
        className: '',
        // 展示隐藏详情(必要)
        showDetail: (item) => {
          item.isShow = !item.isShow
          console.log('1111111111')
        },
        // 详情数据
        items: []
      }
    }
  },
  onLoad(options) {
    // 获取成本单
    this.getData()
  },
  onReachBottom() {
    if (!this.hasMore) return
    this.page++
    this.getData()
  },
  methods: {
    // 点击搜索
    search() {
      this.page = 1
      this.collapseForm.items = []
      this.getData()
    },
    // 获取成本单
    async getData(params) {
      let postForm = {
        ...params,
        purchase_code: this.keywords,
        page: this.page
      }
      const res = await this.$api.prod.other.acceptances(postForm)
      console.log('生产', res)
      if (res.code == 200) {
        let list = []
        res.data.list.forEach((item) => {
          let date = new Date()
          let Y = date.getFullYear(item.purchase_time)
          let M = date.getMonth(item.purchase_time)
          M = M < 10 ? '0' + M : M
          let D = date.getDate(item.purchase_time)
          D = D < 10 ? '0' + D : D
          item.purchase_time = Y + '-' + M + '-' + D
          let pushForm = {
            // 标题
            main_title: item.purchase_code,
            // 副标题
            sub_title: '单据日期：' + (item.purchase_time || ''),
            // 必须传 展开关闭详情
            isShow: false,
            // 展开数据详情
            detailItems: [
              {
                title: '成品',
                value: item.goods_info
              },
              {
                title: '生产数量',
                value: item.number
              },
              {
                title: '验收数量',
                value: item.finish_number
              },
              {
                title: '商品成本',
                value: item.all_money
              },
              {
                title: '委外加工费用',
                value: item.outsourcing_free
              },
              {
                title: '计时工资',
                value: item.hour_free
              },
              {
                title: '计件工资',
                value: item.wages_free
              },
              {
                title: '原材料成本',
                value: item.production_free
              },
              {
                title: '退料成本',
                value: item.material_free
              },
              {
                title: '成本金额',
                value: item.cost_free
              }
            ]
          }
          console.log('pushform', pushForm)
          list.push(pushForm)
        })
        this.collapseForm.items = [...this.collapseForm.items, ...list]
        this.hasMore = res.data.list.length ? true : false
        this.noData = this.collapseForm.items.length ? false : true
        console.log('赋值之后 ', this.collapseForm)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.page {
  padding-top: 92rpx;
  padding-bottom: 50rpx;
}
</style>
